package hash表;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class Solution202 {
    public static void main(String[] args) {

        System.out.println(getSum(2));
        System.out.println(isHappy(2));
    }

    public static boolean isHappy(int n) {
        Set<Integer> map = new HashSet<>();
        int sum = n;
        //sum 如果不是1就继续循环，是1就是快乐数
        while (sum != 1) {
            sum = getSum(sum);
            if (map.contains(sum)) {
                return false;
            } else {
                map.add(sum);
            }
        }
        return true;
    }

    //取数字各个位置平方合
    public static int getSum(int n) {
        int sum = 0;
        while (n != 0) {
            sum += (n % 10) * (n % 10);
            n = n / 10;
        }
        return sum;
    }

}
